<?php
    /**
    * Store global variables [get from cache]
    *
    * @property City $city
    */
    class WebData extends CApplicationComponent
    {
        public $city;
        public $citiesEnable; // only enabled cities
        public $cityAliasesEnable = array(
            'ha-noi',
            'tp-hcm',
            'da-nang',
        );

        public function init(){
            $this->citiesEnable(); 
            $this->setCity();
        }
        
        
        public function citiesEnable(){
            foreach($this->cityAliasesEnable as $cityAlias){
                $this->citiesEnable[$cityAlias] = Yii::app()->cacheData->cityDataAll[$cityAlias];    
            }
            
        }
        
        


        /**
        * detect and set city, cookie
        * 
        * @param mixed $city_alias
        */
        public function setCity($city_alias = NULL){

            $cities = $this->citiesEnable;
            
            $city_alias = $city_alias ? $city_alias : Yii::app()->request->getQuery('city_alias');

            // set city, cookie  by city_alias
            if($city_alias && isset($cities[$city_alias])){
                $this->city = $cities[$city_alias];  

                // init city, cookie
            }else{  

                $city_alias_cookie = Yii::app()->request->cookies['city_alias'];
                
                if($city_alias_cookie && isset($cities[$city_alias_cookie->value])){
                    $this->city = $cities[$city_alias_cookie->value];
                }else{
                    $ip = (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') ? 
                    $_SERVER['REMOTE_ADDR'] : '210.245.80.125';
                    $this->city = Geo::model()->getCityDataByIp($ip);
                    
                    if(!isset($cities[$this->city['alias']])){
                        $this->city = $cities['ha-noi'];     
                    } 
                }  
                   
            }

            Yii::app()->request->cookies['city_alias'] = new CHttpCookie('city_alias', $this->city['alias']);
        }



}